<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once "BaseMapper.php";
class Model_PrivilegesBase{
    protected $_id;
    protected $_name;
    protected $_modules;
    protected $_controller;
    protected $_action;
    
    public function __construct(array $options = NULL)   
    {   
        if (is_array($options)) {   
            $this->setOptions($options);   
        }   
    }   
    public function __set($name, $value)   
    {   
        $method = "set" . $name;   
        if (("mapper" == $name) || !method_exists($this, $method)) {   
            throw new Exception("Invalid Privileges property");   
        }   
        $this->$method($value);   
    }   
    public function __get($name)
    {
        $method = "get" . $name;
        if (("mapper" == $name) || !method_exists($this, $method)) {
            throw new Exception("Invalid Privileges property");
        }
        return $this->$method();
    }
    public function setOptions(array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = "set" . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
    
    public function setId($value){
        $this->_id=$value;
        return $this;
    }
    public function getId(){return $this->_id;}
    
    public function setName($value){
        $this->_name=$value;
        return $this;
    }
    public function getName(){return $this->_name;}
    
    public function setModules($value){
        $this->_modules=$value;
        return $this;
    }
    public function getModules(){return $this->_modules;}
    
    public function setController($value){
        $this->_controller=$value;
        return $this;
    }
    public function getController(){return $this->_controller;}
    
    public function setAction($value){
        $this->_action=$value;
        return $this;
    }
    public function getAction(){return $this->_action;}
}
class Model_DbTable_Privileges extends Zend_Db_Table_Abstract
{
    protected $_name = "privileges";
    protected $_primary = "id";
}
class Model_PrivilegesMapperBase extends BaseMapper{
    protected $_dbTable;
     public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception("Invalid table data gateway provided");
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
 
    public function getDbTable()
    {
        if (NULL === $this->_dbTable) {
            $this->setDbTable("Model_DbTable_Privileges");
        }
        return $this->_dbTable;
    }
    public function save(Model_Privileges $entry){
        $data = array(
                'id' => $entry->getId(),
                'name' => $entry->getName(),
                'modules' => $entry->getModules(),
                'controller'=>$entry->getController(),
                'action'=>$entry->getAction()
                );
         if (null === ($id = $entry->getId())) {
            unset($data["id"]);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array("id = ?" => $id));
        }
    }
    public function delete($id){
        $this->getDbTable()->delete("id=".$id);
    }
    public function find($id, Model_Privileges $entry){
        $result = $this->getDbTable()->find($id);
        if(0== count($result))
        {
            return;
        }
        $row=$result->current();
        $entry->setId($row->id)
                ->setId($row->id)
                ->setName($row->name)
                ->setModules($row->modules)
                ->setController($row->controller)
                ->setAction($row->action);
    }
    public function fetchAll(){
        $result = $this->getDbTable()->fetchAll();
        $entries = array();
        foreach ($result as $row){
            $entry = new Model_Privileges();
            $entry->setId($row->id)
                  ->setId($row->id)
                ->setName($row->name)
                ->setModules($row->modules)
                ->setController($row->controller)
                ->setAction($row->action);
            $entries[]=$entry;
        }
        return $entries;
    }
     public function fetchAllArray() {
        $data = array(array());
        $resultSet = $this->getDbTable()->fetchAll(PDO::FETCH_COLUMN);
        $count = count($resultSet);
        for ($i = 0; $i < $count; $i++) {
            $data[$resultSet[$i]['id']]['name'] = $resultSet[$i]['name'];
        }
        return $data;
    }
    public function fetchPaging($page, $pageSize, $orderBy, $orderDirection, &$totalRecord, $filters=null)
    {
        $db = Zend_Db_Table::getDefaultAdapter();	
		$aFilterValues = array();
		$sFilter = $this->createWhereCondition($filters, $aFilterValues);
		$queryTotal = "SELECT count(*) as totals FROM privileges ".$sFilter."";
		$totalRecord = $this->getTotalRecords($queryTotal, $aFilterValues);
		$query = "SELECT * FROM privileges ".$sFilter." order by ".$orderBy." ".$orderDirection." limit ".($page-1)*$pageSize.",".$pageSize."";
        $stmt = $db->query($query, $aFilterValues);		
        $rows = $stmt->fetchAll(PDO::FETCH_CLASS);
        $stmt->closeCursor();
        $entries   = array();
        foreach ($rows as $row) {
            $entry = new Model_Privileges();
            $entry->setId($row->id)
               ->setId($row->id)
                ->setName($row->name)
                ->setModules($row->modules)
                ->setController($row->controller)
                ->setAction($row->action);
            $entries[] = $entry;
        }
        return $entries;
    }

} 